/*
 * @Author: Thomas
 * @Date: 2021-07-24 18:19:11
 * @LastEditTime: 2021-07-26 00:15:23
 * @LastEditors: Please set LastEditors
 * @Description: SCH ON MCU
 * @FilePath: \sch_on_mcu\sch\inc\sch.h
 */

#ifndef _SCH_H
#define _SCH_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>

#define TASK_MAX 10

#define ENABLE_IDLE_TASK 1
#define RUN_IDLE_TASK_TIME_INTERVAL 2

#define SYS_TICK 1U
#define ENABLE_PREEMPTIVE 0


#define ENABLE_TASK_TIME_RECORD 1
#define TASK_RUN_TIME_MARK uint32_t 

#define TASK_NAME_MAX_LENGTH 32

#define ENABLE_CLI 1
   
#define MAX_TASK_TIMEOUT_CNT 3
  
#define NULL (void*)0
#define NUL '\0'

    typedef struct{
        TASK_RUN_TIME_MARK task_start_time;
        TASK_RUN_TIME_MARK task_end_time;
    } Task_run_time;

    typedef enum
    {
        TASK_NONE,
        TASK_WAITE,
        TASK_RUN,
        TASK_BLOCK
    } TASK_STATE_E;
    typedef enum
    {
        TASK_RUN_ONCE,
        TASK_RUN_CYCLE,
    } TASK_RUN_MODE_E;
    typedef enum
    {
        Cooperative,
        preemptive

    } PREEMPTIVE_E;

    typedef void *(*Entity)(void *);
    typedef void (*idle)(void);

    typedef struct{

        Entity function;
        #if ENABLE_PREEMPTIVE
        int priority;
        #endif
        
        void *parameter;
        int time_start;
        int time_cycle;
        int time_cnt;
        TASK_STATE_E run_state;
        TASK_RUN_MODE_E run_mode;
        char task_name[TASK_NAME_MAX_LENGTH];
        
        #if ENABLE_TASK_TIME_RECORD
        Task_run_time record[ENABLE_TASK_TIME_RECORD];
        #endif
        char task_id;
        char feed_task_cnt;
        
    } Task_S;
#if 1
    typedef struct{
        volatile Task_S *task_p;
        
        int task_cnt;

        idle idle_function;
        
      
        
    }Sys_state_S;
#endif
    int sch_init(uint32_t tick_clock,uint32_t sys_tick_cycle,idle function);
    
    char task_creat(char* task_name,void* parameter_p,
                    #if ENABLE_PREEMPTIVE
                    PREEMPTIVE_E pree,
                    #endif
                    Entity task_function,int wait_time,
                    int time_cycle,TASK_RUN_MODE_E run_mode);

    int task_sch();
    int task_delete(char _id);
    int task_delete_all();
    int sys_tick_call_function();//task update function 
    void feed_task(uint8_t i);


#ifdef __cplusplus
}
#endif

#endif
